import requests

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
    "cookie": "__jdu=317395337; shshshfpa=da9b951d-4b70-1827-1511-eddbfa0839a8-1578640378; qrsc=3; shshshfpb=u8iLV2jQh%20%2FdFe9Z5HsFE8w%3D%3D; unpl=V2_ZzNtbUVQEBJ1XREHLk0JV2IDRV8SVUdAIltOUXJLDgVmUBFVclRCFnQURlRnGlsUZwIZX0ZcQhRFCEdkeB5fA2AFEFlBZxBFLV0CFi9JH1c%2bbRpVSlFKHHQOQ1J8KWwGZzMSXHJVQxN3AEJWeR9sNWAzIm1HXkoQcg12VUsYbEczXxJZQlJGETgKRlJ5EVgHZQUiXHJU; rkv=1.0; __jdv=122270672|baidu|-|organic|not set|1607592693899; areaId=19; ipLoc-djd=19-1601-3633-0; PCSYCityID=CN_440000_440100_440106; __jda=122270672.317395337.1572674846.1607592694.1608122382.18; __jdc=122270672; shshshfp=fc2ad56133bdef82c4cd4b27de678e1b; 3AB9D23F7A4B3C9B=IEATNOIVZSCACPDYAQPVEMQLL2XWEH77MVWHCDU5RNVKLF4QKCFLDPY5352V3KC7Z5WUVFXIN3DVZXST4Z3WNMRMEI"
}


def jd_search(keyword):
    url = "https://search.jd.com/Search"
    params = {
        "keyword": keyword,
        "enc": "utf-8",
        "wq": keyword,
        "psort": 3,
        "click": 0
    }


    response = requests.get(url=url, params=params, headers=headers)
    with open("jd_search.html", "w", encoding="utf-8") as f:
        f.write(response.text)

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
    "cookie": "__jdu=317395337; shshshfpa=da9b951d-4b70-1827-1511-eddbfa0839a8-1578640378; shshshfpb=u8iLV2jQh%20%2FdFe9Z5HsFE8w%3D%3D; unpl=V2_ZzNtbUVQEBJ1XREHLk0JV2IDRV8SVUdAIltOUXJLDgVmUBFVclRCFnQURlRnGlsUZwIZX0ZcQhRFCEdkeB5fA2AFEFlBZxBFLV0CFi9JH1c%2bbRpVSlFKHHQOQ1J8KWwGZzMSXHJVQxN3AEJWeR9sNWAzIm1HXkoQcg12VUsYbEczXxJZQlJGETgKRlJ5EVgHZQUiXHJU; __jdv=122270672|baidu|-|organic|not set|1607592693899; areaId=19; ipLoc-djd=19-1601-3633-0; PCSYCityID=CN_440000_440100_440106; __jdc=122270672; shshshfp=fc2ad56133bdef82c4cd4b27de678e1b; 3AB9D23F7A4B3C9B=IEATNOIVZSCACPDYAQPVEMQLL2XWEH77MVWHCDU5RNVKLF4QKCFLDPY5352V3KC7Z5WUVFXIN3DVZXST4Z3WNMRMEI; __jda=122270672.317395337.1572674846.1608122382.1608126074.19; shshshsID=5b18932a7027312beb05187fd9a8a5f5_3_1608126330353; __jdb=122270672.3.317395337|19.1608126074"
}


def jd_item(keyword):
    url = "https://item.jd.com/100009077475.html"
    params = {
        "keyword": keyword,
        "enc": "utf-8",
        "wq": keyword,
        "psort": 3,
        "click": 0
    }


    response = requests.get(url=url, params=params, headers=headers)
    with open("jd_search2.html", "w", encoding="utf-8") as a:
        a.write(response.text)



if __name__ == "__main__":
    keyword = "iphone12"
    jd_search(keyword)
    jd_item(keyword)
